/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.jitengine.i18nresource;

import com.inspur.edp.web.common.utility.StringUtility;

import java.util.Objects;

/**
 * 生成的i18n资源
 *
 * @author guozhiqi
 */
public class GeneratedI8nResource {
    /**
     * 生成资源项key
     */
    private String key;

    public final String getKey() {
        return key;
    }

    public final void setKey(String value) {
        key = value;
    }

    /**
     * 生成资源项value
     */
    private String value;

    public final String getValue() {
        return value;
    }

    public final void setValue(String value) {
        this.value = value;
    }

//
//    /**
//     * 构造对应的I18n字符串参数
//     * @return
//     */
//    public  String generateI18nString() {
//        return "\"" + this.getKey() + "\":\"" + (StringUtility.isNullOrEmpty(this.getValue()) ? "" : this.getValue()) + "\"";
//    }

    public String generateI18nString() {
        String value = StringUtility.isNullOrEmpty(this.getValue()) ? "" : this.getValue();
        return "\"" + this.getKey() + "\":\"" + escapeJson(value) + "\"";
    }

    /**
     * 转义JSON字符串中的特殊字符
     * @param input 输入字符串
     * @return 转义后的字符串
     */
    private String escapeJson(String input) {
        if (input == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (char c : input.toCharArray()) {
            switch (c) {
                case '"':
                    sb.append("\\\"");
                    break;
                case '\\':
                    sb.append("\\\\");
                    break;
                case '\n':
                    sb.append("\\n");
                    break;
                case '\r':
                    sb.append("\\r");
                    break;
                case '\t':
                    sb.append("\\t");
                    break;
                case '\b':
                    sb.append("\\b");
                    break;
                case '\f':
                    sb.append("\\f");
                    break;
                default:
                    sb.append(c);
                    break;
            }
        }
        return sb.toString();
    }

    /**
     * 不比较value是否相同  仅比较key是否相同，如果key相同
     *
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        //判断地址是否相同
        //判断测试的是否是同一个对象
        //如果是同一个对象，毋庸置疑，它应该返回True
        if (this == obj) {
            return true;
        }
        //非空性
        if (obj == null) {
            return false;
        }
        //判断是否是一个类
        if (obj instanceof GeneratedI8nResource) {
            //向下转型
            //将参数传入的对象造型为Citizen类型
            //object类是所有类的父类不向下转换，父类就比较不了子类的属性
            GeneratedI8nResource p = (GeneratedI8nResource) obj;
            return Objects.equals(this.key, p.getKey()) && Objects.equals(this.value, p.value);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.key, this.value);
    }
}
